/*     */ package com.gopay.bis.gdb.wap.gfbank.payment.merchant;
/*     */ 
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.ObjectInputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.security.MessageDigest;
/*     */ import java.security.PrivateKey;
/*     */ import java.security.Security;
/*     */ import java.security.Signature;
/*     */ import java.sql.Time;
/*     */ import java.util.GregorianCalendar;
/*     */ import java.util.TimeZone;
/*     */ import sun.security.provider.Sun;
/*     */ 
/*     */ public class PLQuery
/*     */ {
/*     */   public static String logfile;
/*     */   public static String urlreq;
/*     */   public static String path;
/*     */   public static String merchid;
/*     */   public static String returl;
/*     */   public static String merchantprivatekey;
/*     */   public static int timeout;
/*     */   public static int clearlog_day;
/*     */   public static int order_max_exist_time;
/*     */   public static int orderid_length;
/*     */   public static PrintStream out;
/*     */ 
/*     */   public static boolean init()
/*     */   {
/*     */     try
/*     */     {
/*  89 */       logfile = PayCfg.getValue("logfile");
/*  90 */       urlreq = PayCfg.getValue("gfbankqueryurl");
/*  91 */       merchid = PayCfg.getValue("merchid");
/*  92 */       path = PayCfg.getValue("orderid_path");
/*  93 */       returl = PayCfg.getValue("merchantreturl");
/*  94 */       merchantprivatekey = PayCfg.getValue("merchantprivatekey");
/*  95 */       timeout = Integer.parseInt(PayCfg.getValue("timeout"));
/*  96 */       if (timeout < 1)
/*  97 */         timeout = 1;
/*  98 */       clearlog_day = Integer.parseInt(PayCfg.getValue("clearlog_day"));
/*  99 */       order_max_exist_time = Integer.parseInt(PayCfg.getValue("order_max_exist_time"));
/* 100 */       if (order_max_exist_time < 1)
/* 101 */         order_max_exist_time = 1;
/* 102 */       orderid_length = Integer.parseInt(PayCfg.getValue("orderid_length"));
/*     */     } catch (Exception e) {
/* 104 */       System.err.println(e);
/* 105 */       return false;
/*     */     }
/*     */     try
/*     */     {
/* 109 */       int d = new GregorianCalendar(TimeZone.getDefault()).get(5);
/* 110 */       if (d == clearlog_day)
/* 111 */         out = new PrintStream(new FileOutputStream(logfile));
/*     */       else
/* 113 */         out = new PrintStream(new FileOutputStream(logfile, true));
/*     */     } catch (Exception e) {
/* 115 */       e.printStackTrace(System.err);
/* 116 */       System.err.println("无法打开日志文件");
/* 117 */       return false;
/*     */     }
/*     */ 
/* 120 */     return true;
/*     */   }
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/* 125 */     if (!init()) {
/* 126 */       return;
/*     */     }
/*     */ 
/*     */     while (true)
/*     */     {
/* 131 */       travel();
/*     */       try
/*     */       {
/* 134 */         Object syn = new Object();
/* 135 */         synchronized (syn)
/*     */         {
/* 137 */           syn.wait(timeout * 60 * 1000);
/*     */         }
/*     */       } catch (Exception e) {
/* 140 */         e.printStackTrace(System.err);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public static void travel()
/*     */   {
/* 147 */     long t = new java.util.Date().getTime();
/* 148 */     File dir = new File(path);
/* 149 */     out.println();
/* 150 */     out.println("Travel path:" + path + " (" + new java.sql.Date(t) + " " + new Time(t) + ")");
/* 151 */     String[] filenames = dir.list();
/*     */ 
/* 153 */     for (int i = 0; i < filenames.length; i++)
/*     */     {
/* 155 */       out.print(filenames[i]);
/* 156 */       File f = new File(path + "/" + filenames[i]);
/* 157 */       long tfile = f.lastModified();
/* 158 */       long tnow = new java.util.Date().getTime();
/* 159 */       out.print(" \t" + new java.sql.Date(tfile) + " " + new Time(tfile));
/* 160 */       out.print(" \t" + new java.sql.Date(tnow) + " " + new Time(tnow));
/* 161 */       out.print(" \t" + (tnow - tfile) / 60000L);
/*     */ 
/* 163 */       if ((tnow - tfile) / 60000L > order_max_exist_time * 60)
/*     */       {
/* 165 */         out.println(" \t已存在" + (tnow - tfile) / 3600000L + "小时, 删除");
/* 166 */         f.delete();
/*     */       }
/*     */       else
/*     */       {
/* 170 */         out.println("");
/*     */ 
/* 172 */         if (filenames[i].length() != orderid_length)
/*     */         {
/* 174 */           out.println("error: file name length !=" + orderid_length + ", filename=" + filenames[i]);
/*     */         }
/*     */         else
/*     */         {
/* 178 */           if ((tnow - tfile) / 60000L <= timeout)
/*     */             continue;
/* 180 */           String signdata = filenames[i] + merchid + returl;
/* 181 */           out.println("signdata:[" + signdata + "]");
/* 182 */           String rs = sign_md(signdata, merchantprivatekey);
/* 183 */           if ((rs == null) || (rs.length() < 10))
/*     */           {
/* 185 */             out.println("签名失败：signdata=[" + signdata + "], retcode=" + rs);
/*     */           }
/*     */           else {
/* 188 */             DoQuery doquery = new DoQuery(urlreq + "?orderid=" + filenames[i] + 
/* 189 */               "&merchid=" + merchid + "&returl=" + returl + "&sign=" + rs, out);
/* 190 */             doquery.start();
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public static String sign_md(String s, String pk) {
/* 197 */     System.err.println("sign_md " + s + " " + pk);
/* 198 */     String s2 = null;
/*     */     try
/*     */     {
/* 201 */       if ((s == null) || (pk == null))
/*     */       {
/* 203 */         System.err.println("Error:in sign_md:innuf or filename is null");
/* 204 */         return "6";
/*     */       }
/*     */       try {
/* 207 */         PrivateKey privatekey = getPrivateKey(pk);
/* 208 */         Sun sun = new Sun();
/* 209 */         Security.addProvider(sun);
/* 210 */         Signature signature = Signature.getInstance("DSA");
/* 211 */         signature.initSign(privatekey);
/* 212 */         MessageDigest messagedigest = MessageDigest.getInstance("SHA");
/* 213 */         byte[] abyte0 = messagedigest.digest(s.getBytes());
/* 214 */         System.err.println("*****: after message digest");
/* 215 */         signature.update(abyte0);
/* 216 */         byte[] abyte1 = signature.sign();
/* 217 */         s2 = byteToHex(abyte1);
/*     */       } catch (Exception e) {
/* 219 */         return "1";
/*     */       }
/*     */ 
/* 222 */       System.err.println("*****: after dsa.sign()");
/*     */     }
/*     */     catch (Exception exception)
/*     */     {
/* 226 */       System.err.println("Exception in sign_md:" + exception.toString());
/* 227 */       exception.printStackTrace();
/* 228 */       return "4";
/*     */     }
/* 230 */     return s2;
/*     */   }
/*     */ 
/*     */   public static PrivateKey getPrivateKey(String keyname)
/*     */   {
/*     */     try
/*     */     {
/* 237 */       FileInputStream istream = new FileInputStream(keyname + ".private");
/* 238 */       ObjectInputStream p = new ObjectInputStream(istream);
/* 239 */       PrivateKey priv = (PrivateKey)p.readObject();
/* 240 */       p.close();
/* 241 */       return priv;
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 245 */       System.err.println("in KeyManager.getPrivateKey()  Caught exception " + e.toString());
/*     */     }
/* 247 */     return null;
/*     */   }
/*     */ 
/*     */   public static String byteToHex(byte[] abyte0)
/*     */   {
/* 252 */     StringBuffer stringbuffer = new StringBuffer();
/* 253 */     for (int i = 0; i < abyte0.length; i++)
/*     */     {
/* 255 */       String s = Integer.toHexString(abyte0[i] & 0xFF);
/* 256 */       if (s.length() != 2)
/* 257 */         stringbuffer.append('0').append(s);
/*     */       else {
/* 259 */         stringbuffer.append(s);
/*     */       }
/*     */     }
/* 262 */     return new String(stringbuffer);
/*     */   }
/*     */ }

/* Location:           D:\桌面\广发银行\B2C支付签名验签工具包\JAVA版\gfgate\WEB-INF\classes\
 * Qualified Name:     gfbank.payment.merchant.PLQuery
 * JD-Core Version:    0.6.0
 */